package com.antdesignbackend.backend.controller;

import com.antdesignbackend.backend.aop.AuthToken;
import com.antdesignbackend.backend.common.returnResult.ReturnCode;
import com.antdesignbackend.backend.common.returnResult.ReturnVO;
import com.antdesignbackend.backend.entity.Button;
import com.antdesignbackend.backend.entity.RoleButton;
import com.antdesignbackend.backend.entity.entityVo.RoleButtonVo;
import com.antdesignbackend.backend.service.ButtonService;
import com.antdesignbackend.backend.service.RoleButtonService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/buttonManage")
public class ButtonController {
    @Autowired
    ButtonService buttonService;
    @Autowired
    RoleButtonService roleButtonService;
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    private String menuId;

    @GetMapping("/index")
    @AuthToken(role_name = {"superAdmin", "admin"})
    public String buttonIndex(String menuId) {
        this.menuId = menuId;
        return "button/index";
    }

    @PostMapping("/tableSearch")
    @ResponseBody
    @AuthToken(role_name = {"superAdmin", "admin"})
    public ReturnVO tableSearch(@RequestBody RoleButtonVo roleButtonVo) {
        ReturnVO returnVO = new ReturnVO();
        try {
            QueryWrapper<RoleButtonVo> queryWrapper = new QueryWrapper<>();
            if (roleButtonVo.getButtonDescription() != null && !roleButtonVo.getButtonDescription().equals(""))
                queryWrapper.eq("b.description", roleButtonVo.getButtonDescription());
            if (roleButtonVo.getMenuName() != null && !roleButtonVo.getMenuName().equals(""))
                queryWrapper.eq("menuName", roleButtonVo.getMenuName());
            if (roleButtonVo.getActivity() >= 0)
                queryWrapper.eq("activity", roleButtonVo.getActivity());
            queryWrapper.eq("rb.roleID", roleButtonVo.getRoleID());

            int total = roleButtonService.count(queryWrapper);

            if (roleButtonVo.getSort() != null && !roleButtonVo.getSort().equals("")) {
                if (roleButtonVo.getSort().equals("id")) roleButtonVo.setSort("b.id");
                if (roleButtonVo.getSortOrder().equals("desc"))
                    queryWrapper.orderByDesc(roleButtonVo.getSort());
                else
                    queryWrapper.orderByAsc(roleButtonVo.getSort());
            }

            List<RoleButtonVo> roleButtonVos = new ArrayList<>();
            if (total > 0)
                roleButtonVos = roleButtonService.selectRoleButtonList(queryWrapper, new Page<>(roleButtonVo.getPageIndex(), roleButtonVo.getPageSize()));


            returnVO.setTotal(total);
            returnVO.setData(roleButtonVos);
            return returnVO;
        } catch (Exception e) {
            logger.error(e.getMessage());
            return new ReturnVO(ReturnCode.FAIL);
        }
    }

    @PostMapping("/edit")
    @ResponseBody
    @AuthToken(role_name = {"superAdmin", "admin"})
    public ReturnVO edit(@RequestBody RoleButtonVo roleButtonVo) {
        try {
            Button button = new Button();
            button.setId(roleButtonVo.getButtonID());
            button.setDescription(roleButtonVo.getButtonDescription());
            button.setActivity(roleButtonVo.getActivity());
            button.setEnDesc(roleButtonVo.getEnDesc());
            button.setMenuID(roleButtonVo.getMenuID());
            button.setMenuName(roleButtonVo.getMenuName());
            buttonService.updateById(button);

            RoleButton roleButton = new RoleButton();
            roleButton.setId(roleButtonVo.getId());
            roleButton.setButtonID(roleButtonVo.getButtonID());
            roleButton.setRoleID(roleButtonVo.getRoleID());
            roleButtonService.updateById(roleButton);

            return new ReturnVO();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return new ReturnVO(ReturnCode.FAIL);
        }
    }

    @PostMapping("/changeState")
    @ResponseBody
    @AuthToken(role_name = {"superAdmin", "admin"})
    public ReturnVO changeState(@RequestBody Button button) {
        try {
            Button model = new Button();
            model.setActivity(button.getActivity());
            model.setId(button.getId());
            buttonService.updateById(button);
            return new ReturnVO();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return new ReturnVO(ReturnCode.FAIL);
        }
    }

    @PostMapping("/add")
    @ResponseBody
    @AuthToken(role_name = {"superAdmin", "admin"})
    public ReturnVO add(@RequestBody RoleButtonVo roleButtonVo) {
        try {
            Button button = new Button();
            button.setDescription(roleButtonVo.getButtonDescription());
            button.setActivity(roleButtonVo.getActivity());
            button.setEnDesc(roleButtonVo.getEnDesc());
            button.setMenuID(roleButtonVo.getMenuID());
            button.setMenuName(roleButtonVo.getMenuName());
            buttonService.save(button);

            RoleButton roleButton = new RoleButton();
            roleButton.setButtonID(button.getId());
            roleButton.setRoleID(roleButtonVo.getRoleID());
            roleButtonService.save(roleButton);

            return new ReturnVO();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return new ReturnVO(ReturnCode.FAIL);
        }
    }

    @PostMapping("/del")
    @ResponseBody
    @AuthToken(role_name = {"superAdmin", "admin"})
    public ReturnVO del(@RequestBody RoleButtonVo roleButtonVo) {
        try {
            buttonService.removeById(roleButtonVo.getButtonID());
            Integer roleID = roleButtonVo.getRoleID();

            QueryWrapper<RoleButton> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("roleID", roleID).eq("buttonID", roleButtonVo.getButtonID());
            roleButtonService.remove(queryWrapper);
            return new ReturnVO();
        } catch (Exception e) {
            logger.error(e.getMessage());
            return new ReturnVO(ReturnCode.FAIL);
        }
    }
}
